<template>  
  <el-card shadow="never" class="aui-card--fill">
    <el-form :inline="true" v-model="SearchForm" clearable  @keydown.enter.native.prevent="getDataList()">
      <el-form-item>
        <el-input v-model="SearchForm.cgyName" placeholder="课题名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">申请课题</el-button>
        <el-button type="danger" @click="del()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <!-- <el-table-column prop="cgyId" header-align="center" align="center" label="课题id">
      </el-table-column> -->
      <el-table-column show-overflow-tooltip width="180"  prop="cgyName" header-align="center" align="center" label="课题名">
      </el-table-column>
      <el-table-column prop="cgyDepart" header-align="center" align="center" label="系别">
      </el-table-column>
      <el-table-column prop="cgyType" header-align="center" align="center" label="题目类型">
      </el-table-column>
      <el-table-column prop="cgyLeadstatus" header-align="center" align="center" label="审核情况">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.cgyLeadstatus === 0" size="small" type="info">未审核</el-tag>
          <el-tag v-if="scope.row.cgyLeadstatus === 1" size="small" type="danger">已驳回</el-tag>
          <el-tag v-if="scope.row.cgyLeadstatus === 2" size="small" type="success">已通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="140" label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="addOrUpdateHandle(scope.row.cgyId,scope.row.cgyName)">修改</el-button>
          <el-button size="small"  type="text" @click="del(scope.row.cgyId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </el-card>
</template>

<script>
import servicesApi from '@/services/servicesApi';
import debounce from 'lodash/debounce';
import AddOrUpdate from '@/views/Teacher/topic-dedeclare'
export default {
  data() {
    return {
      SearchForm: {
        cgyName: '',
        status: null,

      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      deleteList: [],
      deleteList: {
              list: []
           },
      addOrUpdateVisible: false, // 新增／更新，弹窗visible状态
    }
  },
  activated() {
    this.getDataList()
  },
  created() {
    this.getDataList()
  },
  components: {
  AddOrUpdate
},
  methods: {
    addOrUpdateHandle(cgyId, cgyName) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // 当数据被修改后使用这个方法，会回调获取更新后的dom再渲染出来
        this.$refs['addOrUpdate'].init(cgyId, cgyName)
      })
    },
      //删除
      del(id) {
      let roleIds = id ? [id] : this.dataListSelections.map(item => {
      return item.cgyId
    })
    this.deleteList.list = roleIds
      servicesApi.delTtopic(this.deleteList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
              this.$message({
                  message: '删除成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                      this.getDataList()
                  }
              })
          } else {
              this.$message({
                  message: res.data.msg,
                  type: 'danger',
                  duration: 1000,
                  onClose: () => {
                      this.getDataList()
                  }
              })
          }
      })
  },
  //查看详细信息
      detailed(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // this.$refs.addOrUpdate.dataForm.id = id
        this.$refs['addOrUpdate'].init(id)
      })
    },
    // 获取数据列表
    getDataList: debounce(function() {
      this.dataListLoading = true
      let pageInfo = {
        current: this.pageIndex,
        size: this.pageSize,
        name: this.SearchForm.cgyName,
      }
      servicesApi.getTTtopic(pageInfo).then(res => {
        // console.log(res.data)
        let code =res.data.code
        let data = res.data.data
        if (code===200) {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      // console.log('多选')
      // console.log(this.dataListSelections)
    },
    //批准通过
    approve(id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      // console.log(userIds)
      this.$prompt('请输入通过意见', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
      .then(({value}) => {
        // console.log(value)
        this.deleteList = userIds
        let checkList = {
        advise: value,
        list: this.deleteList,
        status: 2
      }
        servicesApi.checkTreport(checkList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
            this.$message({
              type: 'success',
              message: '批准通过成功',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
            this.$message({
            type: 'warning',
            message: '取消批准通过'
          });  
       })
  },
  //驳回
    reject(id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      // console.log(userIds)
      this.$prompt('请输入驳回意见', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
      .then(({value}) => {
        // console.log(value)
        this.deleteList = userIds
        let checkList = {
        advise: value,
        list: this.deleteList,
        status: 1
      }
        servicesApi.checkTreport(checkList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
            this.$message({
              type: 'success',
              message: '驳回成功',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
            this.$message({
            type: 'info',
            message: '取消驳回'
          });  
       })
  },
  }
}
</script>
